package com.forum.jspgenerator;

import java.util.LinkedList;
import java.util.List;

import com.forum.domain.Groups;
import com.forum.domain.Ucg;
import com.forum.domain.UcgDAO;
import com.forum.domain.User;

public class GenerateG {

	private User user;
	private UcgDAO ucgdao;

	// index represent the groups already get
	private int index = 0;
	// the number of the concerned group
	private int count = 0;
	// the group to show each time
	private final int MAXCOUNT = 10;

	List<Groups> grouplist = new LinkedList();
	List<Groups> returnlist = new LinkedList();
	List<Ucg> ucglist = new LinkedList();

	public GenerateG(User user) {
		this.user = user;
	}

	public List<Groups> getGroups() {
		generateGroups();
		return returnlist;
	}

	private void generateGroups() {
		ucglist = ucgdao.findByProperty("user", user);
		for (int i = 0; i < ucglist.size(); i++) {
			Ucg ucg = ucglist.get(i);
			grouplist.add(ucg.getGroups());				
		}
		count = grouplist.size();
		if (count > MAXCOUNT) {
			for (int i = 0; i < MAXCOUNT; i++)
				returnlist.add(grouplist.get(i));
			index = MAXCOUNT;
		} else {
			returnlist = grouplist;
			index = count;
		}
	}

	public List<Groups> moreGroups() {
		
		if (index > count - 1)
			return null;
		else {
			returnlist.clear();
			int currentcount = 0;
			
			if(grouplist.size() < index + MAXCOUNT + 1)
				currentcount = grouplist.size()-index;
			else
				currentcount = MAXCOUNT;
			
			for (int i = 0; i < currentcount; i++) {
				returnlist.add(grouplist.get(i+index));
			}
			index = index + currentcount;
			return returnlist;
		}
	}

	public void setUcgdao(UcgDAO ucgdao) {
		this.ucgdao = ucgdao;
	}

}
